/*
 * Esta clase permite gestionar el acceso de los usuarios
 * Este, va ligado del usuario y el módulo
 * Los accesos son: 1.Visitar 2.Añadir 3. Modificar 4.Total
 */
package person.permission;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.el.ELContext;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import system.general.Exc;
import system.general.Logged;

/**
 *
 * @author emojica
 */
@ManagedBean(name = "access")
@RequestScoped
public class Access {

    Logged logged;
    private Find permissionFind = new Find();
    private List<Beans> permissionList = new ArrayList<>();
    private boolean[] access = new boolean[5];
    private Map<String, String> map = new LinkedHashMap<>();

    /**
     * Creates a new instance of Access
     */
    public Access() {
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        logged = (Logged) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "logged");
        load();
    }

    private void load() {
        try {
            if (logged.getMenuFolder().equals("")) {
                return;
            }

            map.clear();
            map.put("id_persona = " + logged.getLoggedId(), " AND ");
            map.put("nombre = '" + logged.getModuleAccess(), "' ");
            permissionList = permissionFind.byMap(map);
            switch (permissionList.get(0).getAcceso()) {
                case 1: //Visitar
                    access[1] = true;
                    break;
                case 2: //añadir
                    access[1] = true;
                    access[2] = true;
                    break;
                case 3: //modificar
                    access[1] = true;
                    access[2] = true;
                    access[3] = true;
                    break;
                case 4: //total
                    access[1] = true;
                    access[2] = true;
                    access[3] = true;
                    access[4] = true;
                    break;
            }


        } catch (Exc ex) {
            String s = ex.getMessage();
        } catch (SQLException ex) {
            String s = ex.getMessage();
        } catch (Exception ex) {
            String s = ex.getMessage();
        } finally {
        }
    }

    private void clear() {
        for (int i = 0; i < access.length - 1; i++) {
            access[i] = false;
        }
    }

    //Acceso de Visita
    public boolean getVisit() {
        return this.access[1];
    }

    //Acceso de Añadir
    public boolean getAdd() {
        return this.access[2];
    }

    //Acceso de Edición
    public boolean getUpdate() {
        return this.access[3];
    }

    //Acceso Total
    public boolean getDelete() {
        return this.access[4];
    }
}
